<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('申请开票') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('客户名称')" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          :placeholder="$t('客户名称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('发票类型')" prop="invoiceType">
        <el-select v-model="queryParams.invoiceType" filterable :placeholder="$t('发票类型')" clearable>
          <el-option
            v-for="dict in invoiceTypeOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('开票公司')" prop="invoiceCompany">
        <el-select v-model="queryParams.invoiceCompany" filterable :placeholder="$t('开票公司')" clearable>
          <el-option
            v-for="dict in invoiceCompanyOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('开票人')" prop="drawer">
        <el-input
          v-model="queryParams.drawer"
          :placeholder="$t('开票人')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('开票时间')" prop="invoiceTime">
        <DatePicker v-model="dateRange" type="daterange" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['anxin:invoice:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t('新增') }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t("导出") }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="anxinInvoiceApplyList" border>
      <el-table-column fixed="left" :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column fixed="left" :label="$t('客户名称')" align="center" prop="customerName" show-overflow-tooltip min-width="180">
        <template slot-scope="scope">
          <el-link type="primary" @click="updateHandle(scope.row)">{{ scope.row.customerName }}</el-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('项目收款')" align="center" prop="projectCollectionName" show-overflow-tooltip width="180" />
      <el-table-column :label="$t('收款金额') + $t('(元)')" align="center" prop="collectedAmount" show-overflow-tooltip width="180">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.collectedAmount) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('税率(%)')" align="center" prop="taxRate" show-overflow-tooltip width="110" />
      <el-table-column :label="$t('发票类型')" align="center" prop="invoiceType" :formatter="invoiceTypeOptionFormat" show-overflow-tooltip width="110" />
      <el-table-column :label="$t('开票公司')" align="center" prop="invoiceCompany" :formatter="invoiceCompanyOptionFormat" show-overflow-tooltip width="110" />
      <el-table-column :label="$t('开票人')" align="center" prop="applicantName" show-overflow-tooltip width="110" />
      <el-table-column :label="$t('开票时间')" align="center" prop="invoiceTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.invoiceTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" :label="$t('审批状态')" align="center" width="120">
        <template slot-scope="scope">
          <ProcessStatus :status="scope.row.actStatus" :act-id="scope.row.actId" />
        </template>
      </el-table-column>
      <el-table-column fixed="right" :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <!-- 启动流程，终止流程，修改，删除 四个按钮 -->
          <ListBtn :bus-data="scope.row" has-permi="anxin:invoice" style="display: inline-block;" />
          <el-button
            v-hasPermi="['anxin:invoice:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            style="margin-left: 5px;"
            @click="copyHandle(scope.row)"
          >{{ $t('复制') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改申请开票对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <anxinInvoiceApplyForm ref="anxinInvoiceApplyForm" />
    </el-dialog>
  </div>
</template>

<script>
import { listAnxinInvoiceApply, delAnxinInvoiceApply } from '@/api/anxin/invoice/anxinInvoiceApply'
import anxinInvoiceApplyForm from './anxinInvoiceApplyForm.vue'
export default {
    components: {
        anxinInvoiceApplyForm
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 开票类型
          invoiceTypeOptions: [],
          // 开票公司
          invoiceCompanyOptions: [],
          // 申请开票表格数据
          anxinInvoiceApplyList: [],
          // 弹出层标题
          title: '申请开票',
          // 是否显示弹出层
          formOpen: false,
          // 查询参数
          queryParams: {
              customerName: undefined,
              invoiceType: undefined,
              invoiceCompany: undefined,
              drawer: undefined,
              invoiceTime: undefined,
              actTitle: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 日期范围
          dateRange: [],
          // 表单校验
          rules: {
          }
      }
  },
  created() {
      this.getDicts('invoice_type').then(response => {
        this.invoiceTypeOptions = response.data
      })
      this.getDicts('invoice_company').then(response => {
        this.invoiceCompanyOptions = response.data
      })
      this.getList()
  },
  methods: {
    /** 查询申请开票列表 */
    getList() {
        this.loading = true
        listAnxinInvoiceApply(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
            this.anxinInvoiceApplyList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    // 字典翻译
    invoiceTypeOptionFormat(row) {
      return this.selectDictLabel(this.invoiceTypeOptions, row.invoiceType)
    },
    // 字典翻译
    invoiceCompanyOptionFormat(row) {
      return this.selectDictLabel(this.invoiceCompanyOptions, row.invoiceCompany)
    },
    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            customerName: undefined,
            projectCollection: undefined,
            addressPhone: undefined,
            taxpayerNumber: undefined,
            bankAccount: undefined,
            invoiceType: undefined,
            invoiceContent: undefined,
            invoiceCompany: undefined,
            mailingAddress: undefined,
            collectedAmount: undefined,
            drawer: undefined,
            invoiceTime: undefined,
            actId: undefined,
            actStatus: '0',
            actTitle: undefined,
            remark: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined,
            projectCollectionName: undefined,
            isCopy: false
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.dateRange = undefined
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.anxinInvoiceApplyForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        row.isCopy = false
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.anxinInvoiceApplyForm.init(row)
        })
    },
    /** 复制按钮操作 */
    copyHandle(row) {
        this.formOpen = true
        row.isCopy = true
        this.$nextTick(() => {
            // 调用form页面的 copeHandle 方法初始化数据
            this.$refs.anxinInvoiceApplyForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(function() {
        return delAnxinInvoiceApply(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('删除成功'))
      }).catch(function() {
      })
    },
    /** 导出按钮操作 */
    exportHandle() {
      this.downLoadExcelPost('/invoice/anxinInvoiceApply/export', this.queryParams)
    }
  }
}
</script>
